/**
 * 书籍详情
 * @author Qasim <15750783791@163.com>
 * @version 2016/11/1
 */
document.addEventListener('plusready' , ready , false);
var self = memberInfo = description = author = body = litpic = book_name = tid = result = author_brief = price = ''
function ready(){
	self = plus.webview.currentWebview();
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	page = self.page;
	if(!page)page = plus.storage.getItem('index_page');	 
	//获取书籍详情
	$.ajax({  
		type : 'post',
		url : apiRoot + '/Home/Article/bookDetails/',
		data : { 
			id : self.aid,
			uid : memberInfo.aid
		},
		dataType : 'json',
		beforeSend : function(){
			plus.nativeUI.showWaiting('加载中...');
		},
		complete: function(){
			plus.nativeUI.closeWaiting();
		},
		success : function(data){ 
			if(data){
				body = data.body;;
				litpic = data.litpic;
				book_name = data.book_name;
				tid = data.tid;
				author = data.author;
				price = data.price;
				if(book_name)$('.book_name').text(book_name);
				$('.pic').attr('src' , getImgUrl(litpic , '../public/img/book.jpg'))
				if(author)$('.author').text(author);
				if(data.collection == 1){
					$('.want-read').addClass('mui-red');
					$('.want-read').html('&#xe628;')
				}
				
				description = data.description;
				var description_length = description.length;
				if(description_length > 80){
					$('.brief .mui-text-center').css('display' , 'block');
					$('.book_brief').text(description.substr(0 , 80)+'...');
				}else{ 
					$('.book_brief').text(description);
					$('.brief .mui-text-center').css('display' , 'none');
				}
				author_brief = data.author_brief;
				var author_length = author_brief.length;
				if(author_length > 80){  
					$('.autor .mui-text-center').css('display' , 'block');
					$('.author_brief').text(author_brief.substr(0 , 80)+'...');
				}else{
					$('.author_brief').text(author_brief);
					$('.autor .mui-text-center').css('display' , 'none');
				}
				if(data.bookself == 1){ 
					$('.jiaru').text('已放入书架');
				}
			}
		},
		error : function(e){ 
			errorPrompt(e);
		}
	})
	
	comments();
	
	//查看全部简介
	$(".pull").on('click',function(){
		var length = $('.book_brief').text().length;
		if(length < 85){
			$('.book_brief').text(description); 
			$(this).html('&#xe619;')
		}else{
			$('.book_brief').text(description.substr(0 , 80)+'...');
			$(this).html('&#xe61a;')
		}
	}) 
	
	//查看全部作者简介 
	$(".pull1").on('tap',function(){
		var length = $('.author_brief').text().length;
		if(length < 85){
			$('.author_brief').text(author_brief);
			$(this).html('&#xe619;');
		}else{
			$('.author_brief').text(author_brief.substr(0 , 80)+'...'); 
			$(this).html('&#xe61a;')
		}
	})
	
	
	//想读
	$('.collection').on('click' , function(){
 		collection_bookself(0);
	})
	
	//加入书架
	$('.add_bookself').on('tap' , function(){
		collection_bookself(1);
	}) 
	
	
	//查看目录
	$('.catalog').on('click' , function(){
		var parameter = {content : body}; 
    	goUrl('mulv.html' , 'mulv' , parameter)
	})
	
	
	//评价
	$('.add_comment').on('click' , function(){
		var parameter = {aid : self.aid , pages : self.id}; 
    	goUrl('send-comment.html' , 'send_comment' , parameter)
	})
	
	//查看全部
	$('.see_all').on('tap' , function(){
		var parameter = {aid : self.aid , pages : self.id , author:author , litpic:litpic , bookname:book_name , res:result}; 
    	goUrl('pingjia.html' , 'pingjia' , parameter)
	})
	
	$(".free-read").on('tap',function(){
		read(self.aid , true , self.id , book_name , '免费')
	})
}

/**
 * 操作处理  收藏/加入书架
 * @param {Int} type [0 = 收藏  1 = 加入书架] 
 */
function collection_bookself(type){
	$.ajax({  
		type:"post", 
		url:apiRoot + '/Home/Article/collection/',
		data:{
			uid : memberInfo.aid , 
			aid : self.aid ,
			type : type
		},
		beforeSend: function(){
			plus.nativeUI.showWaiting('操作中...');  
		},
		complete:function(){
			plus.nativeUI.closeWaiting(); 
		},
		success : function(data){ 
			if(data == 0){ 
				if(type == 0){
					toast('已取消');  
					$('.want-read').removeClass('mui-red')
					$('.want-read').html('&#xe625;');
				}else{
					toast('已移除');
					$('.jiaru').text('加入书架'); 
				}
			}else{
				if(type == 0){ 
					toast('已收藏');
					$('.want-read').addClass('mui-red')
					$('.want-read').html('&#xe628;')
				}else{ 
					toast('已加入');
					$('.jiaru').text('已放入书架'); 
				}
			}
			
			mui.fire(plus.webview.getWebviewById(page) , 'cancel' , {
				type : type, 
				id : data ,
				aid : self.aid,
				tid : tid,
				litpic:litpic,
				price :price,
				book_name : book_name
			})
		},
		error : function(e){
			errorPrompt(e);
		}
		
	});
}

/**
 * 评价列表
 */
function comments(){
	$.ajax({ 
		type : 'post',
		url : apiRoot + '/Home/Article/comments/',
		data : { 
			id : self.aid,
			uid : memberInfo.aid
		}, 
		success : function(data){ 
			result = eval('('+data+')');
			$('.numbers').text(result.totalNum); 
 			if(result.comments != ''){
 				var html = '';
 				$.each(result.comments , function(k , v){ 
	 				if(k < 3){ 
						html += '<ul class="mui-table-view booklists" data-id="'+v.id+'" data-level="'+v.level+'" data-time="'+v.addtime+'" data-uid="'+v.uid+'"><li class="mui-table-view-cell mui-media">';
						html += '<a href="javascript:;"><img class="mui-media-object touxiang mui-pull-left" src="'+getImgUrl(v.avatar)+'">';
						html += '<div class="mui-media-body"><span class="mui-h5  mui-lan nickname">'+v.nickname+'</span>';
						html += '<span class="mui-pull-right mui-h6">'; 
						html += '<i class="iconfont mui-icon" '+(v.is_goods ? 'style="color:red" data-isGoods="'+v.is_goods+'" ':'')+'>&#xe61d; </i>';
						html += '<span class="goods">'+v.goods+'</span></span>';
						html += '<span class="iconfont gray mlr"><map class="mui-lvs">';
						for(var i = 1 ; i <= v.level ; i++){
							html += '&#xe627;'; 
						}   
						html += '</map></span><p class="words">'+v.content+'</p><p class="time"><span>'+v.number+'</span> 回复</p></div></a></li></ul>';
					} 
 				})
				$('.comment_lists').html(html); 
 				$('.booklists').unbind();
				$('.booklists').on('click' , function(){
					var paramater = {
						page :self.id,
						bookid : self.aid,
						aid:$(this).attr('data-id') , 
						avatar:$(this).find('img').attr('src'),
						nickname:$(this).find('.nickname').text(),
						title : $(this).find('.words').text(),
						goods:$(this).find('.goods').text(),
						level:$(this).attr('data-level'),
						addtime:$(this).attr('data-time'),
						uid : $(this).attr('data-uid'),
						is_goods:$(this).find('.mui-icon').attr('data-isGoods')
					}
					goUrl('pingjia-detail.html' , 'pingjia' ,paramater);
				})
			}else{
				$('.see_all').find('span').text('暂无评价');
			}
		},
		error : function(e){
			errorPrompt(e);
		}
	})
	
}



/**
 * 监听子页面透传数据
 */ 
window.addEventListener('addComment' , function(result){
	comments(); 
})